﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using Entities;
using DataObjects.EFData;
using Utilities;

namespace DataObjects
{
    public class MyCompany : IMyCompany
    {
        private MyCompanyInfo _myCompanyInfo;
        private string mode = string.Empty;
        private WebOnlineEntities datacontext;
        # region Implement Interface
        public MyCompanyInfo MyCompanyInfo
        {
            get
            {
                return _myCompanyInfo;
            }
            set
            {
                _myCompanyInfo = value;
            }
        }

        public string Mode
        {
            get
            {
                return mode;
            }
            set
            {
                mode = value;
            }
        }

        public int Save()
        {
            if (mode == Constants.MODE_INSERT)
            {
                return Insert();
            }
            if (mode == Constants.MODE_UPDATE)
            {
                return Update();
            }
            if (mode == Constants.MODE_INSERT)
            {
                return Delete();
            }
            throw new Exception("Mode is invalid in MyCompany object");
        }

        
        public IList<MyCompanyInfo> GetMyCompanies()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var myCompanyList = datacontext.TB_MyCompany.Select(c => c.ToMyCompanyInfo()).ToList();
                return myCompanyList;
            }
        }

        public MyCompanyInfo GetById(string id)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var GuidId = new Guid(id);
                var myCompany = datacontext.TB_MyCompany.Where(c => c.CompanyId == GuidId).FirstOrDefault();
                if (myCompany != null)
                {
                    return myCompany.ToMyCompanyInfo();
                }
                return null;
            }
        }
        #endregion
        #region Private Methods
        private int Delete()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.DeleteObject(datacontext.TB_MyCompany.FirstOrDefault(c => c.CompanyId == _myCompanyInfo.CompanyId));
                return datacontext.SaveChanges();
            }
        }

        private int Update()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var myCompany = datacontext.TB_MyCompany.Where(c => c.CompanyId == _myCompanyInfo.CompanyId).FirstOrDefault();
                myCompany.Address = _myCompanyInfo.Address;
                myCompany.CompanyName = _myCompanyInfo.CompanyName;
                myCompany.Description = _myCompanyInfo.Description;
                myCompany.Email = _myCompanyInfo.Email;
                myCompany.Fax = _myCompanyInfo.Fax;
                myCompany.Image = _myCompanyInfo.Image;
                myCompany.Phone = _myCompanyInfo.Phone;
                return datacontext.SaveChanges();
            }
        }

        private int Insert()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.AddToTB_MyCompany(this._myCompanyInfo.ToTB_MyCompany());
                return datacontext.SaveChanges();
            }
        }
        #endregion
    }
}
